package de.ryancarmon.validator.validators;

import de.ryancarmon.validator.P;
import de.ryancarmon.validator.ValidatorParam;
import de.ryancarmon.validator.exc.UnsupportedValidationException;
import de.ryancarmon.validator.exc.ValidationException;

public class DoubleValidator extends BaseValidator<Double> {
	@Override
	protected void executeValidation(Double param, ValidatorParam validation) {
		throw new UnsupportedValidationException(validation);
	}

	@Override
	protected void executeValidation(Double param, ValidatorParam validation,
			Object validParam) {
		switch (validation) {
		case EQUALS:
			validateEquals(param, validParam);
			break;
		case LESS:
			validateLess(param, validParam);
			break;
		case GREATER:
			validateGreater(param, validParam);
			break;
		default:
			throw new UnsupportedValidationException(validation);
		}
	}

	protected void validateEquals(Double param, Object equalsObj) {
		if (param == null && equalsObj == null)
			return;
		else if (param == null)
			throw new IllegalArgumentException("'param' may not be null");
		else if (equalsObj == null)
			throw new IllegalArgumentException("'equalsObj' may not be null");

		int equals = toInteger(equalsObj);

		if (param != equals)
			throw new ValidationException(P.EQUALS, equals, param);

	}

	protected void validateLess(Double param, Object lessObj) {
		if (param == null)
			throw new IllegalArgumentException("'param' may not be null");
		if (lessObj == null)
			throw new IllegalArgumentException("'lessObj' may not be null");

		double compare = toDouble(lessObj);

		if (param >= compare)
			throw new ValidationException(P.LESS, compare, param);
	}

	protected void validateGreater(Double param, Object greaterObj) {
		if (param == null)
			throw new IllegalArgumentException("'param' may not be null");
		if (greaterObj == null)
			throw new IllegalArgumentException("'greaterObj' may not be null");

		double compare = toDouble(greaterObj);

		if (param <= compare)
			throw new ValidationException(P.GREATER, compare, param);
	}
}
